/*
* Copyright (c) 2016 sim.com. All Rights Reserved.
*/
package com.cgmnx.admin.web;

import com.google.common.base.Charsets;
import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.concurrent.TimeUnit;

import static org.springframework.util.MimeTypeUtils.TEXT_PLAIN_VALUE;

/**

 */
public class ResponseUtil {

    private static final Logger log = LoggerFactory.getLogger(ResponseUtil.class);

    private static final SimpleDateFormat mFormatGMT = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss zzz", Locale.US);

    private static final long FIVE_MINUTES = TimeUnit.MICROSECONDS.toMinutes(5);

    private static final ThreadLocal<SimpleDateFormat> uniqueNum = new ThreadLocal<SimpleDateFormat>() {

        @Override
        protected SimpleDateFormat initialValue() {
            return mFormatGMT;
        }

    };

    public static void sendMessageNoCache(HttpServletResponse response, String message) {
        response.setContentType("text/html;charset=UTF-8");
        response.setHeader("Cache-Control", "no-cache");
        response.setHeader("Cache-Control", "no-store");
        response.setDateHeader("Expires", 0);
        response.setHeader("Pragma", "no-cache");
        try {
            response.getWriter().print(message);
        } catch (IOException ioe) {
            log.error(ioe.getMessage(), ioe);
            ioe.printStackTrace();
        }
    }

    public static void sendMessageWithCache(HttpServletResponse response, String message) {
        response.setContentType("text/html;charset=UTF-8");
        response.setHeader("Expires", uniqueNum.get().format(new Date(System.currentTimeMillis() + FIVE_MINUTES)));
        response.setHeader("Cache-Control", "public");
        response.setHeader("Cache-Control", "max-age=600");
        try {
            response.getWriter().print(message);
        } catch (IOException ioe) {
            log.error(ioe.getMessage(), ioe);
            ioe.printStackTrace();
        }
    }

    public static void sendJsonNoCache(HttpServletResponse response, String message) {
        response.setContentType("application/json;charset=UTF-8");
        response.setHeader("Cache-Control", "no-cache");
        response.setHeader("Cache-Control", "no-store");
        response.setDateHeader("Expires", 0);
        response.setHeader("Pragma", "no-cache");
        try {
            response.getWriter().print(message);
        } catch (IOException ioe) {
            log.error(ioe.getMessage(), ioe);
            ioe.printStackTrace();
        }
    }

    public static void sendJsonWithCache(HttpServletResponse response, String message) {
        response.setContentType("application/json;charset=UTF-8");
        response.setHeader("Expires", uniqueNum.get().format(new Date(System.currentTimeMillis() + FIVE_MINUTES)));
        response.setHeader("Cache-Control", "public");
        response.setHeader("Cache-Control", "max-age=600");
        try {
            response.getWriter().print(message);
        } catch (IOException ioe) {
            log.error(ioe.getMessage(), ioe);
            ioe.printStackTrace();
        }
    }

    public static void downloadText(HttpServletResponse response, String fileName, InputStream inputStream) {

        String encode = "default.cer";
        try {
            encode = URLEncoder.encode(fileName, Charsets.UTF_8.name());
        } catch (UnsupportedEncodingException e) {
            log.error("编码失败:{}", fileName);
        }

        response.setContentType(TEXT_PLAIN_VALUE);
        response.addHeader("Content-Disposition", "attachment; filename=" + encode);
        try {
            IOUtils.copy(inputStream, response.getOutputStream());
            response.getOutputStream().flush();
        } catch (IOException ex) {
            ex.printStackTrace();
        } finally {
            try {
                IOUtils.closeQuietly(response.getOutputStream());
            } catch (IOException e) {

            }
        }

    }


}